package com.parking.carsystem.parkingchargesystem.view;

import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;


/**
 * Created by dell on 2018/8/29.
 */
public class ToUploadPicDialog extends StarDialogFragment {

    private static final String NAME = "ToUploadPicDialog";

    private View.OnClickListener lsnToCamera;
    private View.OnClickListener lsnToGallery;

    public void setOnClickToCameraListener(View.OnClickListener lsnToCamera) {
        this.lsnToCamera = lsnToCamera;
    }

    public void setOnClickToGalleryListener(View.OnClickListener lsnToGallery) {
        this.lsnToGallery = lsnToGallery;
    }

    @Override
    public String getName() {
        return NAME;
    }

    public ToUploadPicDialog() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置对话框的样式
        setStyle(STYLE_NO_FRAME, R.style.customdialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View containerView = inflater.inflate(R.layout.dialog_slelect_upload_pic_way, container, false);
        TextView txtTitle = (TextView) containerView.findViewById(R.id.txt_select_upload_pic_way_title);
        FrameLayout layoutCamera = (FrameLayout) containerView.findViewById(R.id.layout_upload_with_camera);
        FrameLayout layoutGallery = (FrameLayout) containerView.findViewById(R.id.layout_upload_with_gallery);
        layoutCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lsnToCamera != null) {
                    lsnToCamera.onClick(v);
                }
                dismiss();
            }
        });
        layoutGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lsnToGallery != null) {
                    lsnToGallery.onClick(v);
                }
                dismiss();
            }
        });
        return containerView;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onStart() {
        // 在onStart()里面调用,保证dialog已经完成初始化.

        // 1, 设置对话框的大小
        Window window = getDialog().getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        // 设置宽度为屏幕宽度88%
        window.getAttributes().width = (int) (windowSize.x * 0.88f);
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);

        super.onStart();
    }
}
